package com.moxiao.yfjh.module.system.sms.client;

import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.tea.TeaException;
import com.aliyun.teaopenapi.models.Config;
import com.aliyun.teautil.models.RuntimeOptions;
import com.moxiao.yfjh.framework.common.util.json.JsonUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import static com.aliyun.teautil.Common.assertAsString;


/**
 * @version 1.0
 * @Author zhk
 * @Date 2023/11/10 15:30
 **/
@Component
public class SmsRemoteApi {

    public final static String SIGN = "缘分计划";
    /**
     * 短信验证码
     */
    public final static String TEMPLATE_CODE = " SMS_296345924";
    private final static String ACCESS_KEY_ID = "LTAI5tLuhs1N6gfoCuz1ZT1G";
    private final static String ACCESS_KEY_SECRET = "kTv4dnAOOz3W7AIQXkwHus4t2BNhtU";

    @Value("${aliyun.sms.key-secret}")
    public static String KEY_SECRET;
    @Value("${aliyun.sms.sign-name}")
    public static String SIGN_NAME;
    @Value("${aliyun.sms.key-id}")
    private static String KEY_ID;
    private static Client CLIENT;

    static {
        try {
            String key = "";
            String access = "";
            // 如果取不到配置文件中的值，则使用内置的短信配置
            if (StringUtils.isNotEmpty(KEY_ID)) {
                key = KEY_ID;
            } else {
                key = ACCESS_KEY_ID;
            }
            if (StringUtils.isNotEmpty(KEY_SECRET)) {
                access = KEY_SECRET;
            } else {
                access = ACCESS_KEY_SECRET;
            }
            CLIENT = createClient(key, access);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("阿里云短信服务初始化失败");
        }
    }

    @Value("${aliyun.sms.key-secret}")
    public String KEY_SECRET1;
    @Value("${aliyun.sms.sign-name}")
    public String SIGN_NAME1;
    @Value("${aliyun.sms.key-id}")
    private String KEY_ID1;

    /**
     * 使用AK&SK初始化账号Client
     *
     * @return Client
     * @throws Exception
     */
    public static Client createClient(String key, String access) throws Exception {
        Config config = new com.aliyun.teaopenapi.models.Config()
                // 必填，您的 AccessKey ID
                .setAccessKeyId(key)
                // 必填，您的 AccessKey Secret
                .setAccessKeySecret(access);
        // 必填，表明使用 STS 方式
//                .setType("sts");
        // 访问的域名
        config.endpoint = "dysmsapi.aliyuncs.com";
        return new Client(config);
    }

    public String sendSms(String mobile, String templateParam) {
        String json = "";
        try {
            SendSmsRequest sendSmsRequest = new SendSmsRequest()
                    .setPhoneNumbers(mobile)
                    .setSignName(SIGN)
                    .setTemplateCode(TEMPLATE_CODE)
                    .setTemplateParam(templateParam);
            RuntimeOptions runtime = new RuntimeOptions();

            SendSmsResponse sendSmsResponse = CLIENT.sendSmsWithOptions(sendSmsRequest, runtime);
            json = JsonUtils.toJsonString(sendSmsResponse);
            System.out.println("===================================" + JsonUtils.toJsonString(sendSmsResponse));
            return json;
        } catch (TeaException error) {
            assertAsString(error.message);
        } catch (Exception _error) {
            TeaException error = new TeaException(_error.getMessage(), _error);
            assertAsString(error.message);
        }
        return json;
    }

}
